package parsers;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import object.Department;
import object.Gender;
import object.PersonInfo;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class PersonInfoParser {
	
	private PersonInfo personInfo;
	
	public PersonInfoParser(){
		
	}
	
	public PersonInfoParser(PersonInfo personInfo){
		this.personInfo=personInfo;
	}
	
	public Element parse(Document doc){
		if(doc==null||personInfo==null){
			System.err.println("doc or personinfo cannot be null!");
			return null;
		}
		
		String firstname=personInfo.getFirstname();
		String lastname=personInfo.getLastname();
		Gender gender=personInfo.getGender();
		String mail=personInfo.getMail();
		String tele=personInfo.getTele();
		String country=personInfo.getCountry();
		String province=personInfo.getProvince();
		String city=personInfo.getCity();
		String info=personInfo.getInfo();
		Department department=personInfo.getDepartment();
		Calendar birth=personInfo.getBirth();
		
		Element personInfoE=doc.createElement("个人信息");
		
		Element nameE=doc.createElement("姓名");
		personInfoE.appendChild(nameE);
		Element lastnameE=doc.createElement("姓");
		Text lastnameT=doc.createTextNode(lastname);
		lastnameE.appendChild(lastnameT);
		nameE.appendChild(lastnameE);
		Element firstnameE=doc.createElement("名");
		Text firstnameT=doc.createTextNode(firstname);
		firstnameE.appendChild(firstnameT);
		nameE.appendChild(firstnameE);
		
		Element genderE=doc.createElement("性别");
		personInfoE.appendChild(genderE);
		Text genderT=doc.createTextNode(gender.toString());
		genderE.appendChild(genderT);
		
		Element birthE=doc.createElement("生日");
		personInfoE.appendChild(birthE);
		Text birthT=doc.createTextNode(new SimpleDateFormat("yyyy-MM-dd").format(birth.getTime()));
		birthE.appendChild(birthT);
		
		if(department!=null){
			DepartmentParser dParser=new DepartmentParser(department);
			Element departmentE=dParser.parse(doc);
			personInfoE.appendChild(departmentE);
		}
		
		if(mail!=null){
			Element mailE=doc.createElement("邮箱");
			personInfoE.appendChild(mailE);
			Text mailT=doc.createTextNode(mail);
			mailE.appendChild(mailT);
		}
		
		if(tele!=null){
			Element teleE=doc.createElement("电话");
			personInfoE.appendChild(teleE);
			Text teleT=doc.createTextNode(tele);
			teleE.appendChild(teleT);
		}
		
		if(country!=null&&province!=null&&city!=null){
			Element location=doc.createElement("地区");
			personInfoE.appendChild(location);
			
			Element countryE=doc.createElement("国家");
			location.appendChild(countryE);
			Text countryT=doc.createTextNode(country);
			countryE.appendChild(countryT);
			
			Element provinceE=doc.createElement("省份");
			location.appendChild(provinceE);
			Text provinceT=doc.createTextNode(province);
			provinceE.appendChild(provinceT);
			
			Element cityE=doc.createElement("城市");
			location.appendChild(cityE);
			Text cityT=doc.createTextNode(city);
			cityE.appendChild(cityT);
		}
		
		if(info!=null){
			Element infoE=doc.createElement("自述");
			personInfoE.appendChild(infoE);
			Text infoT=doc.createTextNode(info);
			infoE.appendChild(infoT);
		}
		
		return personInfoE;
		
	}

}
